﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using Microsoft.Win32;
using TEL.ClientClasses;
using TEL.ClientClasses.SipInterfaces;

namespace TEL.TelPhoneF
{
    public class Options : IOptions
    {
        private const string RegistryKeyRun = @"Software\Microsoft\Windows\CurrentVersion\Run";
        private const string RegistryValueNameRun = "TEL.SimplePhone";

        public int AudioIn { get; set; }
        public int AudioOut { get; set; }
        public int NotActiveLines { get; set; }
        public bool RunWithWindows { get; set; }
        public TransferType Transfer { get; set; }
        public int SignalVolume { get; set; }

        public Options()
        {
            AudioIn = Properties.Settings.Default.AudioIn;
            AudioOut = Properties.Settings.Default.AudioOut;
            NotActiveLines = Properties.Settings.Default.NotActiveLines;
            RunWithWindows = LoadRegistryValue();
            Transfer = (TransferType)Properties.Settings.Default.Transfer;
            SignalVolume = Properties.Settings.Default.SignalVolume;

            if (AudioIn > SipOperation.AudioInList().Count - 1) AudioIn = 0;
            if (AudioOut > SipOperation.AudioOutList().Count - 1) AudioOut = 0;
        }

       public void Save()
       {
           Properties.Settings.Default.AudioIn = AudioIn;
           Properties.Settings.Default.AudioOut = AudioOut;
           Properties.Settings.Default.NotActiveLines = NotActiveLines;
           Properties.Settings.Default.Transfer = (int) Transfer;
           Properties.Settings.Default.SignalVolume = SignalVolume;

           Properties.Settings.Default.Save();

           if (RunWithWindows) CreateRegistryValue();
           else DeleteRegistryValue();
       }

        private bool LoadRegistryValue()
        {
            bool res = false;
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyRun, true);
                object o = key.GetValue(RegistryValueNameRun);
                res = o != null;
                key.Close();
            }
            catch (Exception)
            {
            }
            return res;
        }

        private void CreateRegistryValue()
        {
            try
            {
                string file = Application.ExecutablePath;
                RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyRun, true);
                key.SetValue(RegistryValueNameRun, file);
                key.Close();
            }
            catch (Exception)
            {
            }
        }

        private void DeleteRegistryValue()
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyRun, true);
                key.DeleteValue(RegistryValueNameRun);
                key.Close();
            }
            catch (Exception)
            {
            }
        }

        public Options Clone()
        {
            Options res = null;
            string data = string.Empty;

            {
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer serializer = new XmlSerializer(typeof(Options));
                serializer.Serialize(memoryStream, this);
                UTF8Encoding encoding = new UTF8Encoding();
                data = encoding.GetString(memoryStream.ToArray());
            }
            {
                if (string.IsNullOrEmpty(data))
                    throw new Exception("Буфeр пуст при создании копии Options");
                UTF8Encoding encoding = new UTF8Encoding();
                Byte[] byteArray = encoding.GetBytes(data);
                MemoryStream memoryStream = new MemoryStream(byteArray);
                XmlSerializer serializer = new XmlSerializer(typeof (Options));
                res = (Options) serializer.Deserialize(memoryStream);
            }
            return res;
        }
    }
}
